package cn.lfzj.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.lfzj.model.User;

public interface UserDao {

	/**
	 * 根据用户名查用户
	 */
	@Select("SELECT * FROM users WHERE username like concat('%',#{username},'%')")
	public List<User> selByUsername(String username);

	/**
	 * 根据用户名查用户ID
	 */
	@Select("SELECT user_id FROM users WHERE username=#{username}")
	public int selIdByName(String username);

	/**
	 * 根据ID查询用户
	 */
	@Select("SELECT * FROM users where user_id=#{userId}")
	public User selById(int userId);

	/**
	 * 用户名是否存在
	 */
	@Select("SELECT * FROM users WHERE username=#{username}")
	public User selByName(User user);

	/**
	 * 用户注册
	 */
	@Insert("INSERT INTO users(username,password,money) VALUES(#{username},#{password},#{money})")
	public int reg(User user);

	/**
	 * 用户登录
	 */
	@Select("SELECT * FROM users WHERE username=#{username} and password=#{password}")
	public User login(User user);

	/**
	 * 用户充值
	 */
	@Update("UPDATE users SET money=money+#{addMoney} WHERE user_id=#{userId}")
	public int getMoney(@Param("userId") String userId, @Param("addMoney") double addMoney);

	/**
	 * 查询余额
	 */
	@Select("SELECT money FROM users WHERE user_id=#{userId}")
	public double selMoney(int userId);

	/**
	 * 修改账户余额
	 */
	@Update("UPDATE users SET money=money-#{amount} WHERE user_id=#{userId}")
	public int updateMoney(@Param("amount") double amount, @Param("userId") int userId);

	/**
	 * 用户改密
	 */
	@Update("UPDATE users SET password=#{password} WHERE user_id=#{userId} and password=#{oldPassword}")
	public int updPwd(@Param("password") String password, @Param("userId") int userId,
			@Param("oldPassword") String oldPassword);
}
